$(document).ready(function () {


    $.ajax({
            type: 'post',
            url:"http://39.108.233.68:8080/selAllRoute" ,
            success: function (data) {
                console.log(JSON.stringify(data))
                // alert("success");
                for(i in data.data)
                {
                    var tr;
                    tr='<td>'+i+'</td>'+'<td>'+data.data[i].startCity+'</td>'+'<td>'+data.data[i].endCity+'</td>'+'<td>'+data.data[i].routeMes.length+'</td>'+'<td>'+data.data[i].routeMes.describetion+'</td>'+'<td>'+data.data[i].routeMes.days+'</td>'+'<td>'+data.data[i].routeMes. entryfee+'</td>'+'<td>'+'<button id="search_map" class="button" onclick="searchmap('+data.data[i].routeMes.startplaceid+','+data.data[i].routeMes.endplaceid+')">显示路线</button>'+'<button id="change_route" class="button" onclick="jump('+data.data[i].routeMes.id+')">修改</button>'+'<button id="delete_route" class="button" onclick="delete_route('+data.data[i].routeMes.id+')">删除</button>'+'</td>'
                    $("#tbody").append('<tr>'+tr+'</tr>')
                }
            },
            error: function () {
                alert("error");
            }
        }
    )
    }
)

//输入起点终点查询
    $(function () {
        $("#btn_search").click(function () {
            document.getElementById('tbody').innerHTML="";
            var starplace = document.getElementById("starplace").value;
            var endplace = document.getElementById("endplace").value;
            if(starplace != ""&& endplace!="") {

                $.ajax({
                        type: 'post',
                        url:"http://39.108.233.68:8080/selRouteByTwo" ,
                        data: {
                            startCityName:starplace,
                            endCityName :endplace,
                        },
                        datatype: 'json',
                        success: function (data) {
                            // console.log(data)
                            // alert("success");
                            for(i in data.data)
                            {
                                var tr;
                                tr='<td>'+i+'</td>'+'<td>'+data.data[i].startCity+'</td>'+'<td>'+data.data[i].endCity+'</td>'+'<td>'+data.data[i].routeMes.length+'</td>'+'<td>'+data.data[i].routeMes.describetion+'</td>'+'<td>'+data.data[i].routeMes.days+'</td>'+'<td>'+data.data[i].routeMes. entryfee+'</td>'+'<td>'+'<button id="search_map" class="button" onclick="searchmap('+data.data[i].routeMes.startplaceid+','+data.data[i].routeMes.endplaceid+')">显示路线</button>'+'<button id="change_route" class="button" onclick="jump('+data.data[i].routeMes.id+')">修改</button>'+'<button id="delete_route" class="button" onclick="delete_route('+data.data[i].routeMes.id+')">删除</button>'+'</td>'
                                $("#tbody").append('<tr>'+tr+'</tr>')
                            }
                        },
                        error: function () {
                            alert("error");
                        }
                    }
                )
            }


            else {
                alert("请输入起点和终点！")
            }
        })
    })
//查询全部
$(function () {
    $("#searchall_btn").click(function () {
            document.getElementById('tbody').innerHTML="";
            $.ajax({
                    type: 'post',
                    url:"http://39.108.233.68:8080/selAllRoute" ,
                    success: function (data) {
                          console.log(JSON.stringify(data))
                         // alert("success");
                        for(i in data.data)
                        {
                            var tr;
                            tr='<td>'+i+'</td>'+'<td>'+data.data[i].startCity+'</td>'+'<td>'+data.data[i].endCity+'</td>'+'<td>'+data.data[i].routeMes.length+'</td>'+'<td>'+data.data[i].routeMes.describetion+'</td>'+'<td>'+data.data[i].routeMes.days+'</td>'+'<td>'+data.data[i].routeMes. entryfee+'</td>'+'<td>'+'<button id="search_map" onclick="searchmap('+data.data[i].routeMes.startplaceid+','+data.data[i].routeMes.endplaceid+')">显示路线</button>'+'<button id="change_route" onclick="jump('+data.data[i].routeMes.id+')">修改</button>'+'<button id="delete_route" onclick="delete_route('+data.data[i].routeMes.id+')">删除</button>'+'</td>'
                            $("#tbody").append('<tr>'+tr+'</tr>')
                        }
                    },
                    error: function () {
                        alert("error");
                    }
                }
            )
        }
        )
})
//单城市查询
$(function () {
    $("#searchone_btn").click(function () {
        document.getElementById('tbody').innerHTML="";
        var onecity = document.getElementById("search_onecity").value;

        if(onecity!="") {

            $.ajax({
                    type: 'post',
                    url:"http://39.108.233.68:8080/selRoutesByCityName" ,
                    data: {
                        name :onecity,
                    },
                    datatype: 'json',
                    success: function (data) {
                        // console.log(data)
                        // alert("success");
                        for(i in data.data)
                        {
                            var tr;

                            tr='<td>'+i+'</td>'+'<td>'+data.data[i].startCity+'</td>'+'<td>'+data.data[i].endCity+'</td>'+'<td>'+data.data[i].routeMes.length+'</td>'+'<td>'+data.data[i].routeMes.describetion+'</td>'+'<td>'+data.data[i].routeMes.days+'</td>'+'<td>'+data.data[i].routeMes. entryfee+'</td>'+'<td>'+'<button id="search_map" onclick="searchmap('+data.data[i].routeMes.startplaceid+','+data.data[i].routeMes.endplaceid+')">显示路线</button>'+'<button id="change_route" onclick="jump('+data.data[i].routeMes.id+')">修改</button>'+'<button id="delete_route" onclick="delete_route('+data.data[i].routeMes.id+')">删除</button>'+'</td>'
                            $("#tbody").append('<tr>'+tr+'</tr>');
                            console.log(tr);
                        }
                    },
                    error: function () {
                        alert("error");
                    }
                }
            )
        }


        else {
            alert("请输入需要查询的城市！")
        }
    })
})
//删除路线
function delete_route(id) {
     $.ajax({
            type:"post",
            url:"http://39.108.233.68:8080/delOneRoute" ,
            data: {id:id,},
            datatype:'json',
         success: function (data) {
              alert("删除成功");

         },
         error: function () {
             alert("error");
         }
         }
     )
}
function jump(a){
  window.location.href="route_change.html?a="+a;
}
function searchmap(startplaceid,endplaceid) {
    $.ajax({
        type: 'post',
        url:"http://39.108.233.68:8080/selOneCityById",
        async:false,
        data: {
            id:startplaceid

        },
        datatype:'json',
        success: function (data) {

            data1=JSON.stringify(data)
            console.log(data1);

            for (i in data.data)
            {
                cityname=data.data.cityname;
                // console.log(cityid);
            }
            // console.log(cityid);

        },
        error: function () {
            alert("error");
        }
    })
    var startcity=cityname;
    $.ajax({
        type: 'post',
        url:"http://39.108.233.68:8080/selOneCityById",
        async:false,
        data: {
            id:endplaceid,

        },
        datatype:'json',
        success: function (data) {

            data1=JSON.stringify(data)
            console.log(data1);

            for (i in data.data)
            {
                cityname=data.data.cityname;
                // console.log(cityid);
            }
            // console.log(cityid);

        },
        error: function () {
            alert("error");
        }
    })
    var endcity=cityname;
    console.log(startcity);
    console.log(endcity);
    var map = new BMap.Map("container");
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 14);
    var driving = new BMap.DrivingRoute(map, {
        renderOptions: {
            map   : map,
            autoViewport: true
        }
    });
    driving.search(startcity, endcity);
}

function baobiao(){
    window.open("http://39.108.233.68:8080/exportAllRoute");
}
